package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import buildings.House;

import readwrite.Connector;
import users.User;

/**
 * Servlet implementation class ListUsers
 */
@WebServlet("/ListUsers")
public class ListUsers extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connector con;    
	private ArrayList<User> users;
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public ListUsers() throws Exception {
        super();
        con = new Connector();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//int modulId = con.GetLastModulID();
			users = con.getAllUsers();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String answer="";
		String id = "";
		 answer += "<div id=\"Users\">";
		answer +=		"<table> <tr> <td>Username: </td> <td>Access Level: </td><td>Edit User: </td><td>Delete User: </td></tr>";
		for(int i = 0; i < users.size(); i++){
			id="userId"+i;
		answer +=	"  <td> "+users.get(i).getUserName()+" </td><td> "+users.get(i).getAccessLevel()+
				" </td> <td><input type=\"button\" name=\"edit\" id=\"edit\" value=\"Change\" onclick=\"edit('"+i+"')\">"+
				"</td><td><input type=\"button\" name=\"delete\" id=\"delete\" value=\"Delete\" onclick=\"slett('"+i+"')\"></td></tr>";
		answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+users.get(i).getId()+"\">";
		}
		answer += "</table></div>";
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
